//
//  JKToolConfig.swift
//  JKTool
//
//  Created by 姜奎 on 2022/7/22.
//

import Foundation
import Cocoa

public struct JKToolConfig: Decodable,Encodable {
    
    var checkouts: String = "Module/checkouts"
    var builds: String = "Module/Builds"
    var build: String = "build"
    var toolUrl: String = ""
    var completionUrl: String = ""
    var zshCompletionUrl: String = ""
    
    mutating func save() {
        
        if self.checkouts == "" {
            self.checkouts = "Module/checkouts"
        }
        if self.builds == "" {
            self.builds = "Module/Builds"
        }
        if self.build == "" {
            self.build = "build"
        }
        
        
        guard let json = try? JSONEncoder().encode(self) else {
            return
        }
        _ = try? json.write(to: URL(fileURLWithPath: "\(FileManager.DocumnetsDirectory())/config.json"))
    }
    static func read() ->JKToolConfig {
        let url = URL(fileURLWithPath: "\(FileManager.DocumnetsDirectory())/config.json")
        guard let data = try? Data(contentsOf: url),let config = try? JSONDecoder().decode(JKToolConfig.self, from: data) else {
            return JKToolConfig()
        }
        return config
    }
    
    private init() {}
}
